package com.yijiaw.controller;


import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.yijiaw.dao.MessageDAO;
import com.yijiaw.domain.About;
import com.yijiaw.domain.Message;
import com.yijiaw.domain.Part;
import com.yijiaw.domain.Product;
import com.yijiaw.domain.Us;
import com.yijiaw.service.AboutService;
import com.yijiaw.service.MessageService;
import com.yijiaw.service.PartService;
import com.yijiaw.service.ParttypeService;
import com.yijiaw.service.ProductService;
import com.yijiaw.service.ProducttypeService;
import com.yijiaw.service.UsService;

@Controller
public class HomeController {
	
	@Autowired
	MessageDAO messageDAO;
	
	@Autowired
	MessageService messageService;
	
	@Autowired
	UsService usService;
	
	@Autowired
	AboutService aboutService;
	
	@Autowired
	ProductService productService;
	
	@Autowired
	ProducttypeService producttypeService;
	
	@Autowired
	PartService partService;
	
	@Autowired
	ParttypeService parttypeService;
	/**
	 * 首页
	 * @param model
	 * @return
	 */
	@RequestMapping({"/index", "/", "default"})
	public String index(Model model) {
		return "index";
	}
	
	/**
	 * 全部配件页面
	 * @param model
	 * @return
	 */
	@RequestMapping("/hanger")
	public String hanger(Model model) {
		List<Part> list = partService.findAllParts(0, partService.countParts());
		int size = list.size();
		int temp = size / 10;
		int nums = size % 10 != 0 ? temp + 1 : temp;
		model.addAttribute("nums", nums);
		model.addAttribute("list", list);
		
		return "hanger";
	}
	
	@RequestMapping("/hanger/{type}")
	public String hanger(@PathVariable int type, Model model) {
		Set<Part> list = parttypeService.findParttypeByPrimaryKey(type).getParts();
		int size = list.size();
		int temp = size / 10;
		int nums = size % 10 != 0 ? temp + 1 : temp;
		model.addAttribute("nums", nums);
		model.addAttribute("list", list);
		
		return "hanger";
	}
	
	/**
	 * 配件详细
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping("/hanger_a/{id}")
	public String hanger_a(@PathVariable int id, Model model) {
		Part product = partService.findPartByPrimaryKey(id);
		if (product == null) {
			return "404";
		}
		
		int size = product.getProducttype().getParts().size();
		int temp = size / 4;
		int nums = size % 4 != 0 ? temp + 1 : temp;
		model.addAttribute("nums", nums);
		model.addAttribute("product", product);
		
		return "hanger_a";
	}
	
	/**
	 * 关于页面
	 * @param model
	 * @return
	 */
	@RequestMapping("/about")
	public String about(Model model) {
		About about = aboutService.findAllAbouts(0, 1).get(0);
		model.addAttribute("about", about);
		
		return "about";
	}
	
	/**
	 * 联系我们
	 * @param model
	 * @return
	 */
	@RequestMapping("/contact")
	public String contact(Model model) {
		Us us = usService.findAllUss(0, 1).get(0);
		model.addAttribute("us", us);
		
		return "contact";
	}
	
	
	/**
	 * 留言页面
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/message/{page}", method = RequestMethod.GET)
	public String message(@PathVariable int page, Model model) {
		int rows = messageService.countMessages();					//总记录
		if (page < 1 || page > rows) {
			return "redirect:1";
		}
		Us us = usService.findAllUss(0, 1).get(0);
		model.addAttribute("us", us);

		Message message = messageService.findAllMessages(rows - page, 1).get(0);//第一条记录
		int up = page - 6 > 0 ? page - 6 : 1;						//上一页
		int next = page + 6 < rows ? page + 6 : rows;				//下一页
		
		model.addAttribute("page", page);
		model.addAttribute("rows", rows);
		model.addAttribute("message", message);
		model.addAttribute("up", up);
		model.addAttribute("next", next);
		
		return "message";
	}
	
	/**
	 * 保存留言，重定向到留言页面
	 * @param message
	 * @return
	 */
	@RequestMapping(value = "/message", method = RequestMethod.POST)
	public String message(@ModelAttribute Message message) {
		message.setContent("<pre>" + message.getContent() + "</pre>");
		messageService.saveMessage(message);
		
		return "redirect:message/1";
	}
	
	/**
	 * 产品详细
	 * @param model
	 * @return
	 */
	@RequestMapping("/product_a/{id}")
	public String produ(@PathVariable int id ,Model model) {
		Product product = productService.findProductByPrimaryKey(id);
		if (product == null) {
			return "404";
		}
		int size = product.getProducttype().getProducts().size();
		int temp = size / 4;
		int nums = size % 4 != 0 ? temp + 1 : temp;
		model.addAttribute("nums", nums);
		model.addAttribute("product", product);
		
		return "produ_a";
	}
	
	/**
	 * 产品分类
	 * @param model
	 * @return
	 */
	@RequestMapping("product/{type}")
	public String produ_a(@PathVariable int type, Model model) {
		Set<Product> list = producttypeService.findProducttypeByPrimaryKey(type).getProducts();
		int temp = list.size() / 10;
		int nums = list.size() % 10 != 0 ? temp + 1 : temp;
		model.addAttribute("nums", nums);
		model.addAttribute("list", list);
		
		return "produ";
	}
	
	/**
	 * 全部产品
	 * @param model
	 * @return
	 */
	@RequestMapping("/product")
	public String product( Model model) {
		List<Product> list = productService.findAllProducts(0, productService.countProducts());
		int size = list.size();
		int temp = size / 10;
		int nums = size % 10 != 0 ? temp + 1 : temp;
		model.addAttribute("list", list);
		model.addAttribute("nums", nums);
		
		return "produ";
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
}

